 import Page from '../../common/page';
// Page({
// });
const app = getApp();
var sessionKey="";
var openid = wx.getStorageSync("openid");
Page({
    data: {
      active: 'friends',
      hasUserInfo: openid == ""
    },
    onLoad() {
          
    },
    goDetail:function(){
      wx.navigateTo({
      url: '../newcoder/index'
    })
  },
    doAuthorization: function(e) {
      var that = this;
      console.log("调用了 doAuthorization 授权");
      // console.log(e);
      if (e.detail.userInfo == null) { //为null  用户拒绝了授权
        //coding。。。。
        console.log("用户拒绝授权");
      } else {
        //授权
        wx.login({
          success: function(res) {
            console.log('login:code', res.code)
            //发送请求
            wx.request({
              url: "http://localhost:8303/login?code=" + res.code, //接口地址
              method: 'GET',
              header: {
                'content-type': 'application/json' //默认值
              },
              success: function(res) {
                console.log("record  成功", res.data.openid)
                var res = res.data;
                if (res.error) { //发生错误
                  console.log("错误：", res.msg);
                } else { //返回成功
                  try {
                    //wx.setStorageSync("openid",res.data.openid);
                    console.log("是不是undefined",res.openid);
                    wx.setStorageSync('openid', res.openid)
                    openid = wx.getStorageSync("openid");
                    sessionKey = res.sessionKey;
                    console.log("openid",openid);
                  } catch (e) {
                    console.log("wx.login 错误", e);
                  }
                  //加载用户信息g
                  that.loadUserInfo();
                  that.setData({ //设置变量
                    hasUserInfo: false
                  });
                }
              },
              fail: function(err) {
                console.log("record  失败", err);
              }
            })
          }
        })
      }
  
    },
    loadUserInfo: function() {
      var that = this;
      if (openid != "") {
        wx.getUserInfo({
          lang:"zh_CN",
          success: res => {
            console.log("wx获得用户信息:", res);
            that.setData({
              userInfo: res.userInfo
            });
            var data = {
              // "user": res.userInfo,
              "openid":openid,
              "nickName":res.userInfo.nickName,
              "gender":res.userInfo.gender,
              "encryptedData":res.encryptedData,
              "iv":res.iv,
              "rawData":res.rawData,
              "signature":res.signature,
              "gender":res.userInfo.gender,
              "location":res.userInfo.province+" "+res.userInfo.city,
              "avatarUrl":res.userInfo.avatarUrl,
              "sessionKey":sessionKey
            }
            // //发送信息给服务器获得用户信息
            wx.request({
              url: 'http://localhost:8303/info',
              // dataType: "json",
              method: "POST",
              data: data,
              header: {'content-type': 'application/x-www-form-urlencoded'},
              success: function(res) {
                console.log("loadUserInfo（服务器返回） success", res.data);
                if (!res.data.error) {
                  app.globalData.userInfo = res.data.data;
                  that.setData({
                    userInfo: app.globalData.userInfo
                  })
                } else {
                  console.log("服务器获取用戶信息失敗");
                  //TODO：用户信息获取错误
                }
              },
              fail: function(e) {
                console.log("loadUserInfo（服务器返回）error", e);
                //TODO:错误
              },
              complete: function(e) {
                //完成
              }
            })
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            if (this.userInfoReadyCallback) {
              this.userInfoReadyCallback(res)
            }
          }
        })
      }
    },
  
    // 事件处理函数
    bindViewTap: function() {
      wx.navigateTo({
        url: '../logs/logs'
      })
    },
    onShow: function() {
      var that = this;
      console.log("openid:", openid);
      that.loadUserInfo();
    },
    onChange(event) {
      // event.detail 的值为当前选中项的索引
      var index = event.detail
      console.log(index);
      var that =this;
      if (index == 'home') {
       // 首页页面标识
       that.setData({
        active:'home'
      })
      // 分类，使用 navigateTo跳转之后页面左上角是返回按钮
      wx.switchTab({
        url: '../grid/index',
      });
      // wx.switchTab({
      //   url: '../grid/index',
      // });
      that.setData({
        active:'home'
      })
      } else if (index == 'friends') {
        ///that.data.active = 'friends'
        that.setData({
          active:'friends'
        })
        // 分类，使用 navigateTo跳转之后页面左上角是返回按钮
        // wx.redirectTo({
        //   url: '../col/index',
        // })
        wx.switchTab({
          url: '../col/index',
        })
        that.setData({
          active:'friends'
        })
      } 
    },
  })
  